<?php
function page_ballot_portal() {
  $advpolls = customize_advpoll_hottest_list();
  return theme('customize_advpoll_portal', $advpolls);
}

function page_advpoll_hottest() {
  $advpolls = customize_advpoll_hottest_list();
  return theme('customize_advpoll_portal', $advpolls);
}

function page_advpoll_recent() {
  $advpolls = customize_advpoll_recent_list();
  return theme('customize_advpoll_portal', $advpolls);
}

function page_advpoll_self() {
  global $user;

  $advpolls = customize_advpoll_user_list($user->uid);
  return theme('customize_advpoll_portal', $advpolls);
}

function page_advpoll_create() {
//print_pre(node_get_types());
  require_once(drupal_get_path('module', 'node') . '/node.pages.inc');
  return node_add('advpoll-binary');
}

/**
 * Get hottest advpoll list.
 *
 * @return array.
 * array(
 *  0 => object('title'=>'xx'),
 *  1 => object('title'=>'xx'),
 * )
 */
function customize_advpoll_hottest_list() {
  /**
   * SELECT n.nid, n.title, p.active, n.created, SUM(c.chvotes) AS votes
   * FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid
   * INNER JOIN {poll_choices} c ON n.nid = c.nid WHERE n.status = 1
   * GROUP BY n.nid, n.title, p.active, n.created
   * ORDER BY votes DESC
   */

  $polls = array();
  $limit = 10;

  $sql = "SELECT advpoll.nid, advpoll.active, node.title, node.created, COUNT(details.content_id) AS num
          FROM advpoll advpoll
          LEFT JOIN node node ON node.nid=advpoll.nid
          LEFT JOIN votingapi_vote details ON details.content_id=advpoll.nid
          WHERE advpoll.`active`=1 AND node.status = 1
          GROUP BY nid
          ORDER BY num DESC";
  $sql = db_rewrite_sql($sql);
  // Count all polls for the pager.
  $count_sql = db_rewrite_sql("SELECT COUNT(*) FROM {node} n INNER JOIN {advpoll} p ON n.nid = p.nid WHERE n.status = 1");

  $result = pager_query($sql, $limit, 0, $count_sql);
  while ($node = db_fetch_object($result)) {
    $polls[] = $node;
  }

  return $polls;
}

/**
 * Get recent advpoll list.
 *
 * @return array.
 * array(
 *  0 => object('title'=>'xx'),
 *  1 => object('title'=>'xx'),
 * )
 */
function customize_advpoll_recent_list() {
  $polls = array();
  $limit = 10;

  $sql = "SELECT advpoll.nid, advpoll.active, node.title, node.created, COUNT(details.content_id) AS num
          FROM advpoll advpoll
          LEFT JOIN node node ON node.nid=advpoll.nid
          LEFT JOIN votingapi_vote details ON details.content_id=advpoll.nid
          WHERE advpoll.`active`=1 AND node.status = 1
          GROUP BY nid
          ORDER BY created DESC";
  $sql = db_rewrite_sql($sql);
  // Count all polls for the pager.
  $count_sql = db_rewrite_sql("SELECT COUNT(*) FROM {node} n INNER JOIN {advpoll} p ON n.nid = p.nid WHERE n.status = 1");

  $result = pager_query($sql, $limit, 0, $count_sql);
  while ($node = db_fetch_object($result)) {
    $polls[] = $node;
  }

  return $polls;
}

/**
 * Get recent advpoll list.
 *
 * @return array.
 * array(
 *  0 => object('title'=>'xx'),
 *  1 => object('title'=>'xx'),
 * )
 */
function customize_advpoll_user_list($uid) {
  $polls = array();
  $limit = 10;

  $sql = "SELECT advpoll.nid, advpoll.active, node.title, node.created, COUNT(details.content_id) AS num
          FROM advpoll advpoll
          LEFT JOIN node node ON node.nid=advpoll.nid
          LEFT JOIN votingapi_vote details ON details.content_id=advpoll.nid
          WHERE advpoll.`active`=1 AND node.status = 1 AND node.uid=".$uid."
          GROUP BY nid
          ORDER BY created DESC";
  $sql = db_rewrite_sql($sql);
  // Count all polls for the pager.
  $count_sql = db_rewrite_sql("SELECT COUNT(*) FROM {node} n INNER JOIN {advpoll} p ON n.nid = p.nid WHERE n.status = 1");

  $result = pager_query($sql, $limit, 0, $count_sql);
  while ($node = db_fetch_object($result)) {
    $polls[] = $node;
  }

  return $polls;
}